const Path = require('path');
const Fs = require('fs');
const IP = require('ip');
const express = require('express');
const app = express();
let plugin = 'plugin';
plugin = 'plugin-behavior';
let dir = Path.join(__dirname, plugin);

app.all('*', (req, res, next) => {
    let ext = Path.extname(req.url);
    if (ext === '.js') {
        res.header('Content-Type', 'application/x-javascript;charset=utf-8');
    } else {
        res.header('Content-Type', 'application/json;charset=utf-8');
    }
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'X-Requested-With');
    res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS');
    res.header('X-Powered-By', ' 3.2.1');
    res.header('Content-Security-Policy', `connect-src *`);
    next();
});
app.use(express.static(dir));

let pluginFile = 'addon.json';
if (!Fs.existsSync(Path.join(dir, pluginFile))) {
    console.log(`识别不到插件：${pluginFile}`);
    return;
}
const port = 5231;
let server = app.listen(port, () => {
    let ip = 'localhost';//IP.address();
    console.log(`http://${ip}:${port}/${pluginFile}`);
});
